package com.itheima.io1.mytest;

import java.io.*;

/**
 * @author zhangbaiyan
 * @date 2024-09-20 19:19
 * @description: 拷贝文件夹
 * @version: 1.0
 */
public class copyDirs {
    public static void main(String[] args) throws IOException {
            File src = new File("C:\\Users\\17685\\Desktop\\简历");
            File dst = new File("C:\\Users\\17685\\Desktop\\copy");

                copyDirs(src,dst);



    }

    private static void copyDirs(File src, File dst) throws IOException {
        dst.mkdirs();
        //递归
        //1.进入数据源
        File[] f = src.listFiles();
        //2.遍历数组
        if (f != null) {
            for (File file : f) {
                if (file.isFile()) {
                    //3.判断文件,拷贝
                    FileInputStream fis = new FileInputStream(file);
                    FileOutputStream fos = new FileOutputStream(new File(dst,file.getName()));
                     byte[] b = new byte[1024];
                     int len;
                     while ((len = fis.read(b))!=-1){
                         fos.write(b,0,len);
                     }

                     fos.close();
                     fis.close();
                } else {
                    //4.判断文件夹,递归
                   copyDirs(file, new File(dst,file.getName()));
                }
            }
        }
    }
}

